/**
 * 游戏部分
 */
namespace GameFrame {
    export class Manager extends Singleton {
        private _scene = {};
        private _currKey;

        get Scene(): typeof Scene {
            return Scene;
        }
        get GameObject(): typeof GameObject {
            return GameObject;
        }
        get Component(): typeof GameFrame.Component {
            return GameFrame.Component;
        }
        get ComponentSystem(): ComponentSystem {
            return ComponentSystem.instance();
        }
        //注册场景
        registScene($key, $scene) {
            this._scene[$key] = $scene;
        }
        runScene($key, ...param: any[]) {
            let nowScene = this._scene[$key];
            if (!nowScene) {
                Frame.debugConsole(this, `场景${$key}不存在`);
                return;
            }
            let oldScene = this._scene[this._currKey];
            if (oldScene) {
                oldScene.onExit();
            }
            nowScene.onEnter.apply(nowScene, param);
            this._currKey = $key;
        }

        /**
         * 创建动画工厂
         * @param $resName 资源名
         * @param $dataName 数据名
         * @param $txtrType 贴图资源格式
         */
        getMCData($resName, $dataName, $txtrType = "png") {
            let MCF = Frame.DataPool.getData(`MCFatory_${$resName}`);
            let MCD = Frame.DataPool.getData(`MCData_${$dataName}`);
            if (!MCD) {
                if (!MCF) {
                    let data = RES.getRes(`${$resName}_json`);
                    let txtr = RES.getRes(`${$resName}_${$txtrType}`);
                    MCF = new egret.MovieClipDataFactory(data, txtr);
                    Frame.DataPool.storeData(`MCFatory_${$resName}`, MCF);
                }
                MCD = (<egret.MovieClipDataFactory>MCF).generateMovieClipData($dataName);
                Frame.DataPool.storeData(`MCData_${$dataName}`, MCD);
            }
            return MCD;
        }
    }
    /**
     * 场景基类
     */
    class Scene {
        private _layerList = [];
        private isInit: boolean = false;
        //添加需要的显示层
        protected openLayer($layer) {
            Frame.Display.stage.addChild($layer);
            this._layerList.push($layer);
        }

        //runScene时触发
        onEnter() {
            if (!this.isInit) {
                this.init();
                this.isInit = true;
            }
            this.open();
        }
        //每次打开scene
        protected open() {
            //打开显示层 this.openLayer
            //打开视图 this.openView
        }
        //第一次打开scene
        protected init() {
            //注册控制器 Frame.MVC.registModule
        }
        //切出Scene时触发
        onExit() {
            for (let i of this._layerList) {
                i.removeChildren();
            }
            this.quit();
        }
        protected quit() { }

        protected loadResAndOpenView($key, $layer, $resGrpName, ...param) {
            Frame.RES.loadGroup($resGrpName,
                () => { this.openView($key, $layer, ...param); }, () => { }, this);
        }
        //打开视图
        protected openView($key, $layer, ...param) {
            let controller = Frame.MVC.getController($key);
            controller.openView($layer, ...param);
        }

        //关闭一个layer上的全部界面
        protected closeViews($layer) {
            for (let i of this._layerList) {
                if (i == $layer) {
                    i.removeChildren();
                }
            }
        }
    }
    /**
     * 游戏对象基类
     * by wyy
     */
    class GameObject extends Frame.Display.EuiView {
        protected component = {};
        constructor(skinName = "resource/game_skins/gameScene/GameObjectSkin.exml") {
            super(skinName);
        }
        //挂载组件
        addComponent(comKey, option = {}) {
            let com = Frame.Game.ComponentSystem.getComponent(comKey);
            com.addEntity(this, option);
            this.component[comKey] = com;
            com.start();
        }
        removeComponent(comKey) {
            let com = this.component[comKey];
            if (!com) {
                Frame.debugConsole(this, `组件${comKey}未挂载在${this}上`);
                return;
            }
            com.removeEntity(this);
            this.component[comKey] = null;

        }
    }
    /**
     * 组件挂载管理系统
     * by wyy
     */
    class ComponentSystem extends Singleton {
        components = {};
        constructor() {
            super();
        }
        registComponent(key, com: Component) {
            this.components[key] = com;
        }
        getComponent(key) {
            if (this.components[key]) {
                return this.components[key];
            }
            else {
                Frame.debugConsole(this, `组件${key}不存在`);
                return null;
            }
        }
    }
    /**
     * 游戏组件基类
     */
    export class Component {
        //挂载的实体集合
        public entity = [];
        public isRuning: boolean;
        public type: number;

        //间隔执行
        protected delay: number = 10;

        public constructor() {
        }

        public start(): void {
            this.isRuning = true;
            Ticker.getInstance().registerTick(this.update, this, this.delay);
        }

        public stop(): void {
            this.entity = null;
            this.isRuning = false;
            this.type = null;
        }

        protected update(advancedTime: number): void {
            for (let i of this.entity) {
                this.deal(i["entity"], i["option"]);
            }
        }
        protected deal(entity, option) {

        }
        addEntity(entity, option = {}) {
            this.entity.push({ entity: entity, option: option });
        }
    }

}